package com.zufangbao.earth.web.controller.particalsystem;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.core.web.resolver.Secure;
import com.zufangbao.earth.PageView;
import com.zufangbao.earth.RoleSpec;
import com.zufangbao.earth.entity.particalsystem.Partical;
import com.zufangbao.earth.entity.particalsystem.ParticalModel;
import com.zufangbao.earth.entity.security.Principal;
import com.zufangbao.earth.service.AppParticlesService;
import com.zufangbao.earth.service.ParticalModelService;
import com.zufangbao.earth.service.ParticalService;
import com.zufangbao.gluon.handler.BaseViewSpecHandler;
import com.zufangbao.gluon.handler.ResultHandler;
import com.zufangbao.gluon.spec.earth.GlobalSpec4Earth;
import com.zufangbao.gluon.spec.earth.GlobalSpec4Earth.ErrorCode4ParticalSystem;
import com.zufangbao.gluon.spec.global.GlobalCodeSpec.GeneralErrorCode;
import com.zufangbao.gluon.spec.global.GlobalMsgSpec.GeneralErrorMsg;

@Controller("particalController")
@RequestMapping("/particalsystem")
@MenuSetting("menu-system")
public class ParticalController {
	
	public static Log logger = LogFactory.getLog(ParticalController.class);
	
	@Qualifier("viewSpecHandler")
	@Autowired
	private BaseViewSpecHandler redirectSpecHandler;
	
	@Autowired
	private ParticalService particalService;
	
	@Autowired
	private ParticalModelService particalModelService;
	
	@Autowired
	private ResultHandler resultHandler;
	
	@Autowired
	private AppParticlesService appParticleService;
	
	@RequestMapping("/partical-all-list")
	@MenuSetting("submenu-partical")
	public ModelAndView getAllParticals(@Secure Principal principal,Page page){
		
		try {
			if(!principal.getAuthority().equals(RoleSpec.ROLE_SUPER_USER)){
				
				return redirectSpecHandler.noAuthorityPageSpec();
			}
			
			List<Partical> particalList = particalService.list(Partical.class, page);
			
			ModelAndView modelAndView = new ModelAndView(PageView.PAGE_LOAD_ALL_PARTICALS);
			
			modelAndView.addObject("particalList", particalList);
			
			return modelAndView;
			
		} catch (Exception e) {
			
			logger.error("get all particals error: " + e);
			
			return redirectSpecHandler.errorSpec();
		}
	}
	
	@RequestMapping("/partical-update")
	public @ResponseBody String updateSinglePartical(@Secure Principal principal,Partical partical, @RequestParam("originParticalId")String originParticalId){
		
		try
		{
		if(!principal.getAuthority().equals(RoleSpec.ROLE_SUPER_USER)){
			
			return resultHandler.errorJsonResult(GeneralErrorCode.ERROR_NO_AUTHORITY,GeneralErrorMsg.MSG_NO_AUTHORITY);
		}
		if(partical.isNotValid()){
			
			return resultHandler.errorJsonResult(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS,GeneralErrorMsg.MSG_ILLEGAL_REQ);
		}
		String particalUniqueId  = particalService.updateSinglePartical(partical, originParticalId);
		
		particalModelService.updateParticalModelWithUniqueId(originParticalId, particalUniqueId);
		
		appParticleService.updateAppParticleWithUniqueId(originParticalId, particalUniqueId);
		
		return resultHandler.sucJsonResult("particalUniqueId", particalUniqueId);
		
		}
		catch (Exception e) {
			
			logger.error("update partical error: " + e);
			
			return resultHandler.errorJsonResult(ErrorCode4ParticalSystem.ERROR_PARTICAL_SYSTEM,GeneralErrorMsg.MSG_SYSTEM_ERROR);
		}
	}

	@RequestMapping(value = "/delete-partical",method = RequestMethod.POST)
	public @ResponseBody String deleteSingleParticalAndModles(@Secure Principal principal,
												@RequestParam("particalUniqueId")String particalUniqueId) {
		
		try {
			if(!principal.getAuthority().equals(RoleSpec.ROLE_SUPER_USER)){
				
				return resultHandler.errorJsonResult(GeneralErrorCode.ERROR_NO_AUTHORITY,GeneralErrorMsg.MSG_NO_AUTHORITY);
			}
			if(StringUtils.isEmpty(particalUniqueId)) {
				
				return resultHandler.errorJsonResult(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS,GeneralErrorMsg.MSG_ILLEGAL_REQ);
			}
			
			particalService.deleteParticalAndModelsByUniqueId(particalUniqueId);
			
			particalModelService.delValueByParticalUniqueId(particalUniqueId);
			
			return resultHandler.sucJsonResult();
			
		}
		catch (Exception e) {
			
			logger.error("delete particals error: " + e);
			
			return resultHandler.errorJsonResult(ErrorCode4ParticalSystem.ERROR_PARTICAL_SYSTEM,GeneralErrorMsg.MSG_SYSTEM_ERROR);
		}
	}

	@RequestMapping("/delete-partical-models")
	public @ResponseBody String deleteParticalModels(@Secure Principal principal, @RequestParam("particalModelIds")String particalModelIds) {
		
		try {
			if(!principal.getAuthority().equals(RoleSpec.ROLE_SUPER_USER)){
				
				return resultHandler.errorJsonResult(GeneralErrorCode.ERROR_NO_AUTHORITY,GeneralErrorMsg.MSG_NO_AUTHORITY);
			}
			if(StringUtils.isEmpty(particalModelIds)) {
				
				return resultHandler.errorJsonResult(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS,GeneralErrorMsg.MSG_ILLEGAL_REQ);
			}
			List<Long> particalModelIdList = null;

			particalModelIdList = JsonUtils.parseArray(particalModelIds,Long.class);
			
			if(particalModelIdList==null){	
						
				return resultHandler.errorJsonResult(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS,GeneralErrorMsg.MSG_ILLEGAL_REQ);
			}
			particalModelService.delValueFromIds(particalModelIdList);
			
			return resultHandler.sucJsonResult();
		}
		catch (Exception e) {
			
			logger.error("delete ParticalModels error: " + e);
			
			return resultHandler.errorJsonResult(ErrorCode4ParticalSystem.ERROR_PARTICAL_SYSTEM,GeneralErrorMsg.MSG_SYSTEM_ERROR);
		}
	}

	@RequestMapping("/update-partical-modles")
	public  @ResponseBody String updateParticalModels(@Secure Principal principal, @RequestParam("particalModels")String particalModels) {
	
		try {
			if(!principal.getAuthority().equals(RoleSpec.ROLE_SUPER_USER)){
				
				return resultHandler.errorJsonResult(GeneralErrorCode.ERROR_NO_AUTHORITY,GeneralErrorMsg.MSG_NO_AUTHORITY);
			}
			if(StringUtils.isEmpty(particalModels)) {
				
				return resultHandler.errorJsonResult(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS,GeneralErrorMsg.MSG_ILLEGAL_REQ);
			}
			List<ParticalModel> particalModelIdList = null;
				
			particalModelIdList = JsonUtils.parseArray(particalModels,ParticalModel.class);
			
			if(particalModelIdList==null){	
					
				return resultHandler.errorJsonResult(GlobalSpec4Earth.ERROR_ILLEGAL_PARAMS,GeneralErrorMsg.MSG_ILLEGAL_REQ);
			}
			particalModelService.updateFromParticalModels(particalModelIdList);
			
			return resultHandler.sucJsonResult();
		}
		catch (Exception e) {
			
			logger.error("Update ParticalModels error: " + e);
			
			return resultHandler.errorJsonResult(ErrorCode4ParticalSystem.ERROR_PARTICAL_SYSTEM,GeneralErrorMsg.MSG_SYSTEM_ERROR);
		}
	}

	@RequestMapping("/partical-details")
	@MenuSetting("submenu-partical")
	public ModelAndView getParticalDetails(@Secure Principal principal,
			@RequestParam(value = "particalUniqueId", required = false) String particalUniqueId) {
		
		try {
			if(!principal.getAuthority().equals(RoleSpec.ROLE_SUPER_USER)){
				
				return redirectSpecHandler.noAuthorityPageSpec();
			}
			
			ModelAndView modelAndView = new ModelAndView(PageView.PAGE_PARTICAL_DETAILS);
			
			Partical partical  = particalService.getParticalByUniqueId(particalUniqueId);
			
			List<ParticalModel> particalModelList = particalModelService.loadAllParticalModels(particalUniqueId);
			
			modelAndView.addObject("partical", partical);
			
			modelAndView.addObject("particalModels", particalModelList);
			
			return modelAndView;
			
		} catch (Exception e) {
			
			logger.error("get partical details  error: " + e);
			
			return redirectSpecHandler.errorSpec();
		}

	}
}
